﻿using Laharsub.Server.Wcf;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Globalization;

namespace Laharsub.Server.Wcf.Test
{
    
    
    /// <summary>
    ///This is a test class for LaharsubTest and is intended
    ///to contain all LaharsubTest Unit Tests
    ///</summary>
    [TestClass()]
    public class LaharsubTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Start & Stop of the WCF HTTP service
        ///</summary>
        [TestMethod()]
        public void StartStopTest()
        {
            string originalBaseAddress = Laharsub_Accessor.parameterDefaults["baseAddress"];
            try
            {
                // avoid a namespace conflict with a running instance of Laharsub
                Laharsub_Accessor.parameterDefaults["baseAddress"] = "http://" + Environment.MachineName + "/" + Guid.NewGuid().ToString();
                Laharsub.Start();
                Laharsub.Stop();
            }
            finally
            {
                Laharsub_Accessor.parameterDefaults["baseAddress"] = originalBaseAddress;
            }
        }

        /// <summary>
        ///A test for InitializeBackend
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Laharsub.exe")]
        public void InitializeBackendTest()
        {
            Laharsub_Accessor.InitializeBackend();
            Assert.IsNotNull(Laharsub_Accessor.Backend);
            Assert.IsTrue(typeof(InMemoryPubSubBackend) == Laharsub_Accessor.Backend.GetType());
        }

        /// <summary>
        ///A test for GetParamAsString for all default parameter values
        ///</summary>
        [TestMethod()]
        public void GetParamAsStringTest()
        {            
            //{ "baseAddress", "http://" + Environment.MachineName + "/ps/memory" },
            //{ "faultExceptionEnabled", "true" },
            //{ "helpEnabled", "true" },
            //{ "maxConcurrentCalls", Int32.MaxValue.ToString(CultureInfo.InvariantCulture) },
            //{ "serverPollTimeoutInS", "45" },
            //{ "InMemoryPubSubBackend.maxMemoryMessageStoreInMB", "10" },
            //{ "InMemoryPubSubBackend.maxMessageTtlInS", "15" },
            //{ "backendType", "Laharsub.Server.Wcf.InMemoryPubSubBackend" }

            Assert.AreEqual("http://" + Environment.MachineName + "/ps/memory", Laharsub.GetParamAsString("baseAddress"));
            Assert.AreEqual("true", Laharsub.GetParamAsString("faultExceptionEnabled"));
            Assert.AreEqual("true", Laharsub.GetParamAsString("helpEnabled"));
            Assert.AreEqual(Int32.MaxValue.ToString(CultureInfo.InvariantCulture), Laharsub.GetParamAsString("maxConcurrentCalls"));            
            Assert.AreEqual("45", Laharsub.GetParamAsString("serverPollTimeoutInS"));
            Assert.AreEqual("10", Laharsub.GetParamAsString("InMemoryPubSubBackend.maxMemoryMessageStoreInMB"));
            Assert.AreEqual("15", Laharsub.GetParamAsString("InMemoryPubSubBackend.maxMessageTtlInS"));
            Assert.AreEqual("Laharsub.Server.Wcf.InMemoryPubSubBackend", Laharsub.GetParamAsString("backendType"));
        }

        /// <summary>
        ///A test for GetParamAsBool for all default parameters
        ///</summary>
        [TestMethod()]
        public void GetParamAsBoolTest()
        {
            Assert.AreEqual(true, Laharsub.GetParamAsBool("faultExceptionEnabled"));
            Assert.AreEqual(true, Laharsub.GetParamAsBool("helpEnabled"));
        }

        /// <summary>
        ///A test for GetParamAsInt for all default parameters
        ///</summary>
        [TestMethod()]
        public void GetParamAsIntTest()
        {
            Assert.AreEqual(Int32.MaxValue, Laharsub.GetParamAsInt("maxConcurrentCalls"));
            Assert.AreEqual(45, Laharsub.GetParamAsInt("serverPollTimeoutInS"));
            Assert.AreEqual(10, Laharsub.GetParamAsInt("InMemoryPubSubBackend.maxMemoryMessageStoreInMB"));
            Assert.AreEqual(15, Laharsub.GetParamAsInt("InMemoryPubSubBackend.maxMessageTtlInS"));
        }
    }
}
